package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.paas.pojo.workflow.WorkflowExplainPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.WorkflowExplainUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.pojo.nodes.InsertRecordNode;
import com.xbongbong.workflow.service.operator.WorkflowOperatorMainAndSubFormService;
import com.xbongbong.workflow.service.operator.WorkflowOperatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 此方法主要是用来解决主字段赋值给主字段
 * @author 吴峰
 * @date 2021/06/24 9:45
 */
@Service("workflowOperatorMainServiceImpl")
public class WorkflowOperatorMainServiceImpl implements WorkflowOperatorMainAndSubFormService<JSONObject> {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowOperatorMainServiceImpl.class);

    @Resource
    private Map<Integer, WorkflowOperatorService>  workflowOperatorMap;


    @Override
    public JSONObject getInsertFieldListByDataNode(WorkflowOperationsPojo workflowOperationsPojo, WorkflowExplainPojo workflowExplainPojo, FieldAttrEntity targetField,
                                                   Map<Long, PaasFormDataEntityExt> formDataMap , Map<Long, JSONObject> dataMap, InsertRecordNode insertRecordNode,
                                                   WorkflowNodeEntity workflowNodeEntity, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPojo) throws XbbException {

        JSONObject result = new JSONObject();
        FieldAttrEntity fieldAttrEntity = WorkflowExplainUtil.getThisField(workflowDataPojo.getExplainMap(), workflowOperationsPojo.getFormId(),
                workflowOperationsPojo.getBusinessType(), workflowOperationsPojo.getValueType(), workflowOperationsPojo.getValueAttr());

        WorkflowOperatorService workflowOperatorService = workflowOperatorMap.get(workflowOperationsPojo.getValueType());
        Object value = workflowOperatorService.operatorByInsertNodeSourceData(workflowNodeEntity,  fieldAttrEntity, targetField, formDataMap, dataMap,
                workflowOperationsPojo, workflowDataPojo, workflowTransferPojo, workflowExplainPojo);
        result.put(workflowOperationsPojo.getAttr(), value);
        return result;
    }


    @Override
    public Object getUpdateData(WorkflowOperationsPojo workflowOperationsPojo, WorkflowExplainPojo workflowExplainPojo, FieldAttrEntity targetField,
                                Map<Long, PaasFormDataEntityExt> formDataMap, Map<Long, JSONObject> dataMap, WorkflowNodeEntity workflowNodeEntity, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPojo) throws XbbException {

        WorkflowOperatorService workflowOperatorService = workflowOperatorMap.get(workflowOperationsPojo.getValueType());
        FieldAttrEntity fieldAttrEntity = WorkflowExplainUtil.getThisField( workflowDataPojo.getExplainMap(), workflowOperationsPojo.getFormId(),
                workflowOperationsPojo.getBusinessType(), workflowOperationsPojo.getValueType(), workflowOperationsPojo.getValueAttr());
        return workflowOperatorService.operatorByUpdateNode(workflowNodeEntity, fieldAttrEntity, targetField, formDataMap, dataMap,
                workflowOperationsPojo, workflowDataPojo, workflowTransferPojo, workflowExplainPojo);
    }
}
